﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_FIM_PetitionDtls : IDAL_FIM_PetitionDtls
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();
        

        public D_FIM_PetitionDtls()
        {

        }

        public IList<M_FIM_PetitionDtls> GetFull(Nullable<int> petition_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_PetitionDtls> infoList = new List<M_FIM_PetitionDtls>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@petition_seq", petition_seq);

                strSQL = "SELECT petd.petition_dtls_seq, petd.petition_seq"
                    + ", petd.money_req_seq, petd.bgm_plan_seq, petd.job_site_seq"
                    + ", petd.amount, petd.amount_tax, petd.amount_penalty"
                    + ", petd.amount_redemption, petd.amount_fund, petd.amount_real"
                    + ", mr.money_req_name"//, plan.bgm_plan_name
                    + ", j.job_site_name"
                    //+ ", mr.money_req_name AS petition_dtls_name"
                    + ", mr.contact_seq, c.contact_name"
                    + ", mr.event_seq"
                    + ", pay.pay_seq"

                    + ", petd.record_status"
                    + ", petd.created_by, petd.created_when, petd.updated_by, petd.updated_when"
                    + " FROM fim_petition_dtls petd"
                    + " LEFT JOIN fim_money_requests mr ON petd.money_req_seq = mr.money_req_seq"
                    //+ " LEFT JOIN bgm_plans plan ON petd.bgm_plan_seq = plan.bgm_plan_seq"
                    + " LEFT JOIN fim_mas_contacts c ON mr.contact_seq = c.contact_seq"
                    + " LEFT JOIN hrm_mp_job_sites j ON petd.job_site_seq = j.job_site_seq"
                    + " LEFT JOIN fim_payments pay on petd.petition_dtls_seq = pay.petition_dtls_seq"
                        + " and pay.record_status not like @delete_status"
                    + " WHERE petd.record_status not like @delete_status"
                    + (petition_seq != null ? " and petd.petition_seq = @petition_seq" : "")
                    + " ORDER BY petd.petition_dtls_seq";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_PetitionDtls info = new M_FIM_PetitionDtls();
                        info.petition_dtls_seq = Convert.ToInt32(drc[i]["petition_dtls_seq"]);
                        info.petition_seq = cn.ConvertNullInt(drc[i]["petition_seq"]);
                        info.money_req_seq = cn.ConvertNullInt(drc[i]["money_req_seq"]);
                        info.bgm_plan_seq = cn.ConvertNullInt(drc[i]["bgm_plan_seq"]);
                        info.job_site_seq = cn.ConvertNullInt(drc[i]["job_site_seq"]);
                        info.amount = cn.ConvertNullDouble(drc[i]["amount"]);
                        info.amount_tax = cn.ConvertNullDouble(drc[i]["amount_tax"]);
                        info.amount_penalty = cn.ConvertNullDouble(drc[i]["amount_penalty"]);
                        info.amount_redemption = cn.ConvertNullDouble(drc[i]["amount_redemption"]);
                        info.amount_fund = cn.ConvertNullDouble(drc[i]["amount_fund"]);
                        info.amount_real = cn.ConvertNullDouble(drc[i]["amount_real"]);

                        info.money_req_name = drc[i]["money_req_name"].ToString();
                        //info.bgm_plan_name = drc[i]["bgm_plan_name"].ToString();
                        info.job_site_name = drc[i]["job_site_name"].ToString();
                        //info.petition_dtls_name = drc[i]["petition_dtls_name"].ToString();
                        info.contact_seq = cn.ConvertNullInt(drc[i]["contact_seq"]);
                        info.contact_name = drc[i]["contact_name"].ToString();
                        info.event_seq = cn.ConvertNullInt(drc[i]["event_seq"]);
                        info.pay_seq = cn.ConvertNullInt(drc[i]["pay_seq"]);

                        info.record_status = drc[i]["record_status"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_FIM_PetitionDtls> GetList(Nullable<int> petition_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_PetitionDtls> infoList = new List<M_FIM_PetitionDtls>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@petition_seq", petition_seq);

                strSQL = "SELECT petd.petition_dtls_seq"
                    + ", mr.money_req_name AS petition_dtls_name"
                    + " FROM fim_petition_dtls petd"
                    + " LEFT JOIN fim_money_requests mr ON petd.money_req_seq = mr.money_req_seq"
                    + " LEFT JOIN bgm_plans plan ON petd.bgm_plan_seq = plan.bgm_plan_seq"
                    + " WHERE petd.record_status not like @delete_status"
                    + (petition_seq != null ? " and petd.petition_seq = @petition_seq" : "")
                    + " ORDER BY petd.petition_dtls_seq";
                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;

                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_PetitionDtls info = new M_FIM_PetitionDtls();
                        info.petition_dtls_seq = Convert.ToInt32(drc[i]["petition_dtls_seq"]);
                        info.petition_dtls_name = drc[i]["petition_dtls_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_FIM_PetitionDtls GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_FIM_PetitionDtls info = new M_FIM_PetitionDtls();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT petd.petition_dtls_seq, petd.petition_seq"
                    + ", petd.money_req_seq, petd.bgm_plan_seq, petd.job_site_seq"
                    + ", petd.amount, petd.amount_tax, petd.amount_penalty"
                    + ", petd.amount_redemption, petd.amount_fund, petd.amount_real"
                    + ", mr.money_req_name"
                    //+ ", plan.bgm_plan_name"
                    + ", j.job_site_name"
                    + ", mr.money_req_name AS petition_dtls_name"

                    + ", petd.record_status"
                    + ", petd.created_by, petd.created_when, petd.updated_by, petd.updated_when"
                    + " FROM fim_petition_dtls petd"
                    + " LEFT JOIN fim_money_requests mr ON petd.money_req_seq = mr.money_req_seq"
                    //+ " LEFT JOIN bgm_plans plan ON petd.bgm_plan_seq = plan.bgm_plan_seq"
                    + " LEFT JOIN hrm_mp_job_sites j ON petd.job_site_seq = j.job_site_seq"
                    + " WHERE petd.petition_dtls_seq = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.petition_dtls_seq = Convert.ToInt32(drc[0]["petition_dtls_seq"]);
                    info.petition_seq = cn.ConvertNullInt(drc[0]["petition_seq"]);
                    info.money_req_seq = cn.ConvertNullInt(drc[0]["money_req_seq"]);
                    info.bgm_plan_seq = cn.ConvertNullInt(drc[0]["bgm_plan_seq"]);
                    info.job_site_seq = cn.ConvertNullInt(drc[0]["job_site_seq"]);
                    info.amount = cn.ConvertNullDouble(drc[0]["amount"]);
                    info.amount_tax = cn.ConvertNullDouble(drc[0]["amount_tax"]);
                    info.amount_penalty = cn.ConvertNullDouble(drc[0]["amount_penalty"]);
                    info.amount_redemption = cn.ConvertNullDouble(drc[0]["amount_redemption"]);
                    info.amount_fund = cn.ConvertNullDouble(drc[0]["amount_fund"]);
                    info.amount_real = cn.ConvertNullDouble(drc[0]["amount_real"]);

                    info.money_req_name = drc[0]["money_req_name"].ToString();
                    //info.bgm_plan_name = drc[0]["bgm_plan_name"].ToString();
                    info.job_site_name = drc[0]["job_site_name"].ToString();
                    info.petition_dtls_name = drc[0]["petition_dtls_name"].ToString();

                    info.record_status = drc[0]["RECORD_STATUS"].ToString();
                    info.created_by = cn.ConvertNullInt(drc[0]["CREATED_BY"]);
                    info.created_when = cn.ConvertNullDateTime(drc[0]["CREATED_WHEN"]);
                    info.updated_by = cn.ConvertNullInt(drc[0]["UPDATED_BY"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["UPDATED_WHEN"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(M_FIM_PetitionDtls info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(14);

                dbManager.AddParameters(0, "@petition_dtls_seq", info.petition_dtls_seq);
                dbManager.AddParameters(1, "@petition_seq", info.petition_seq);
                dbManager.AddParameters(2, "@money_req_seq", info.money_req_seq);
                dbManager.AddParameters(3, "@bgm_plan_seq", info.bgm_plan_seq);
                dbManager.AddParameters(4, "@job_site_seq", info.job_site_seq);
                dbManager.AddParameters(5, "@amount", info.amount);
                dbManager.AddParameters(6, "@amount_tax", info.amount_tax);
                dbManager.AddParameters(7, "@amount_penalty", info.amount_penalty);
                dbManager.AddParameters(8, "@amount_redemption", info.amount_redemption);
                dbManager.AddParameters(9, "@amount_fund", info.amount_fund);
                dbManager.AddParameters(10, "@amount_real", info.amount_real);

                dbManager.AddParameters(11, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(12, "@created_by", info.created_by);
                dbManager.AddParameters(13, "@created_when", info.created_when);

                strSQL = "INSERT INTO fim_petition_dtls"
                    + "(petition_seq, money_req_seq, bgm_plan_seq, job_site_seq"
                    + ", amount, amount_tax, amount_penalty, amount_redemption, amount_fund, amount_real"
                    + ", record_status, created_by, created_when)"
                    + " VALUES (@petition_seq"
                        + ", @money_req_seq"
                        + ", @bgm_plan_seq"
                        + ", @job_site_seq"
                        + ", @amount"
                        + ", @amount_tax"
                        + ", @amount_penalty"
                        + ", @amount_redemption"
                        + ", @amount_fund"
                        + ", @amount_real"

                        + ", @record_status"
                        + ", @created_by"
                        + ", @created_when"
                        + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_FIM_PetitionDtls info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(13);

                dbManager.AddParameters(0, "@petition_dtls_seq", info.petition_dtls_seq);
                dbManager.AddParameters(1, "@petition_seq", info.petition_seq);
                dbManager.AddParameters(2, "@money_req_seq", info.money_req_seq);
                dbManager.AddParameters(3, "@bgm_plan_seq", info.bgm_plan_seq);
                dbManager.AddParameters(4, "@job_site_seq", info.job_site_seq);
                dbManager.AddParameters(5, "@amount", info.amount);
                dbManager.AddParameters(6, "@amount_tax", info.amount_tax);
                dbManager.AddParameters(7, "@amount_penalty", info.amount_penalty);
                dbManager.AddParameters(8, "@amount_redemption", info.amount_redemption);
                dbManager.AddParameters(9, "@amount_fund", info.amount_fund);
                dbManager.AddParameters(10, "@amount_real", info.amount_real);

                dbManager.AddParameters(11, "@updated_by", info.updated_by);
                dbManager.AddParameters(12, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_petition_dtls"
                    + " SET petition_seq = @petition_seq"
                    + ", money_req_seq = @money_req_seq"
                    + ", bgm_plan_seq = @bgm_plan_seq"
                    + ", job_site_seq = @job_site_seq"
                    + ", amount = @amount"
                    + ", amount_tax = @amount_tax"
                    + ", amount_penalty = @amount_penalty"
                    + ", amount_redemption = @amount_redemption"
                    + ", amount_fund = @amount_fund"
                    + ", amount_real = @amount_real"

                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE petition_dtls_seq = @petition_dtls_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(M_FIM_PetitionDtls info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@petition_dtls_seq", info.petition_dtls_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_petition_dtls"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE petition_dtls_seq = @petition_dtls_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
